package com.seveneight.baijiesteward.ui.main.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.base.MessageBean;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.BankBean;
import com.seveneight.baijiesteward.bean.BranchBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.user.contract.GetBankListContract;
import com.seveneight.baijiesteward.ui.user.fragment.IdentifyFragment;
import com.seveneight.baijiesteward.ui.user.model.AddCreditCardModel;
import com.seveneight.baijiesteward.ui.user.presenter.GetBankListPresenter;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.utils.WindowUtil;
import com.seveneight.baijiesteward.views.BankListDialog;
import com.seveneight.baijiesteward.views.BranchBankListDialog;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class AddCreditCardActivity extends BaseActivity implements View.OnClickListener,GetBankListContract.GetBankView{

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.credit_card_number)
    EditText credit_card_number;
    @BindView(R.id.user_name)
    TextView user_name;
    @BindView(R.id.cvn2)
    EditText cvn2;
    @BindView(R.id.catd_validity_date)
    EditText catd_validity_date;
    @BindView(R.id.bank_name)
    TextView bank_name;
    @BindView(R.id.bill_date)
    TextView bill_date;
    @BindView(R.id.pay_date)
    TextView pay_date;
    @BindView(R.id.phone)
    EditText phone;

    String BankId;
    private BankListDialog bankListDialog;
    private BranchBankListDialog branchBankListDialog;
    GetBankListContract.GetBankPresenter getBankPresenter;
    List<BranchBean> dateData=new ArrayList<>();
    List<BankBean> cardData;
    BranchBean dateBean1;
    BranchBean dateBean2;
    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_add_credit_card);
    }

    @Override
    protected void initView() {
        tv_title.setText("添加信用卡");
    }

    @Override
    protected void initData() {
        user_name.setText(userBean.getRealName());
        getBankPresenter = new GetBankListPresenter(this);
        getBankPresenter.getBankList(this,jsonFormat.commonParam());
        addDate();
    }
    public void addDate(){
        for (int i=1;i<29;i++){
            BranchBean bean=new BranchBean();
            bean.setCode(i+"");
            bean.setKey(i+"");
            bean.setText("每月"+i+"号");
            dateData.add(bean);
        }
    }

    @Override
    protected void setListener() {

    }
    @Subscribe
    public void onEvent(MessageBean messageBean){
        switch (messageBean.getType()){
            case 11:
                dateBean1 = (BranchBean) messageBean.getData();
                bill_date.setText("每月"+dateBean1.getKey()+"号");
                break;
            case 12:
                dateBean2 = (BranchBean) messageBean.getData();
                pay_date.setText("每月"+dateBean2.getKey()+"号");
                break;
        }
    }

    @OnClick({R.id.iv_back,R.id.bt_next,R.id.bill_date_layout,R.id.bill_date,R.id.pay_date_layout,R.id.pay_date,R.id.bank_name_layout,R.id.bank_name})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_next:
//                new MaterialDialog.Builder(this)
//                        .content("添加信用卡即做一笔10元的消费交易作为验证信用卡的正确性")
//                        .contentColorRes(R.color.graytext)
//                        .backgroundColorRes(R.color.white)
//                        .negativeColorRes(R.color.mainblue)
//                        .negativeText(R.string.negative)
//                        .positiveColorRes(R.color.mainblue)
//                        .positiveText(R.string.positive)
//                        .onPositive(new MaterialDialog.SingleButtonCallback() {
//                            @Override
//                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                                dialog.dismiss();
//                            }
//                        })
//                        .build().show();

                addCard();
                break;
            case R.id.bill_date:
                branchBankListDialog = new BranchBankListDialog(activity, 11, dateData);
                branchBankListDialog.show();
                break;
            case R.id.bill_date_layout:
                branchBankListDialog = new BranchBankListDialog(activity, 11, dateData);
                branchBankListDialog.show();
                break;
            case R.id.pay_date_layout:
                branchBankListDialog = new BranchBankListDialog(activity, 12, dateData);
                branchBankListDialog.show();
                break;
            case R.id.pay_date:
                branchBankListDialog = new BranchBankListDialog(activity, 12, dateData);
                branchBankListDialog.show();
                break;
            case R.id.bank_name_layout:
                if (bankListDialog == null) {
                    bankListDialog = new BankListDialog(this, cardData, bank_name, BankId);
                }
                bankListDialog.show();
                break;
            case R.id.bank_name:
                if (bankListDialog == null) {
                    bankListDialog = new BankListDialog(this, cardData, bank_name,BankId);
                }
                bankListDialog.show();
                break;
        }
    }
    public void addCard(){
        if (StringUtils.isEmpty(credit_card_number.getText().toString())){
            Toast.makeText(activity, "请填写信用卡号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (null == dateBean2){
            Toast.makeText(activity, "请选择还款日", Toast.LENGTH_SHORT).show();
            return;
        }
        if (null == dateBean1){
            Toast.makeText(activity, "请选择账单日", Toast.LENGTH_SHORT).show();
            return;
        }
        if (null == bankListDialog){
            Toast.makeText(activity, "请选择发卡银行", Toast.LENGTH_SHORT).show();
            return;
        }else {
            if (StringUtils.isEmpty(bankListDialog.getBankId())){
                Toast.makeText(activity, "请选择发卡银行", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        Map<String,String> params=new HashMap<>();
        params.put("BankID",bankListDialog.getBankId());//银行id
        params.put("CreditCardNo",credit_card_number.getText().toString());//信用卡号
        params.put("RealName",userBean.getRealName());//真实名
        params.put("IdCardNo",userBean.getIdCardNo());//身份证号
        params.put("BankPhone",phone.getText().toString());//手机号
        params.put("EffectiveDate",catd_validity_date.getText().toString());//信用卡有效期
        params.put("CardCvn2",cvn2.getText().toString());//背面三个数字
        params.put("BillDate",dateBean1.getKey());//账间日
        params.put("RepayDate",dateBean2.getKey());//还款日
        new AddCreditCardModel().getBankList(this, jsonFormat.AddCard(params), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                Toast.makeText(getContext(),"添加成功",Toast.LENGTH_SHORT).show();
                setResult(RESULT_OK,new Intent());
                Bundle bundle5 = new Bundle();
                bundle5.putString("url", anyBean.getBindInfo());
                WindowUtil.getInstance().startActivity(getContext(), WebActivity.class, bundle5);
                finish();
            }

            @Override
            public void onFailed() {
            }
        });

    }

    @Override
    public void rebackBankList(List<BankBean> data) {
        cardData=data;
    }
}
